package com.hqyj.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.goods.dto.GoodsInfo;
import com.hqyj.goods.dto.InventoryInfo;
import com.hqyj.goods.entity.Goods;
import com.hqyj.goods.entity.Inventory;
import com.hqyj.goods.mapper.GoodsMapper;
import com.hqyj.goods.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.suppliers.entity.Suppliers;
import com.hqyj.utils.PageUtil;
import com.hqyj.utils.ResultUtils;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sdx2009
 * @since 2022-09-30
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public ResultUtils inventoryIndex(InventoryInfo inventoryInfo) {
        Page<Goods> page = new Page<>(inventoryInfo.getPage(),inventoryInfo.getRow());
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.orderByDesc("goods_join_time");
        qw.like(inventoryInfo.getGoodsName() != null && !inventoryInfo.getGoodsName().equals(""),"goods_name",inventoryInfo.getGoodsName());
        qw.like(inventoryInfo.getGoodsType() != null && !inventoryInfo.getGoodsType().equals(""),"goods_type",inventoryInfo.getGoodsType());
        qw.like(inventoryInfo.getGoodsUserName() != null && !inventoryInfo.getGoodsUserName().equals(""),"goods_user_name",inventoryInfo.getGoodsUserName());
        qw.like(inventoryInfo.getGoodsSuppliersName() != null && !inventoryInfo.getGoodsSuppliersName().equals(""),"goods_suppliers_name",inventoryInfo.getGoodsSuppliersName());
        Page<Goods> pageList= this.page(page, qw);
        PageUtil<Goods> pageUtil = new PageUtil<>();
        pageUtil.setData(pageList.getRecords());
        pageUtil.setTotal(pageList.getTotal());
        pageUtil.setTotalPage(pageList.getPages());
        return new ResultUtils(200,"",pageUtil);
    }

    @Override
    public ResultUtils inventoryOperation(InventoryInfo inventoryInfo) {
        Page<Inventory> page = new Page<>(inventoryInfo.getPage(),inventoryInfo.getRow());
        Page<Inventory> pageList = goodsMapper.inventoryOperation(page, inventoryInfo);
        PageUtil<Inventory> pageUtil = new PageUtil<>();
        pageUtil.setData(pageList.getRecords());
        pageUtil.setTotal(pageList.getTotal());
        pageUtil.setTotalPage(pageList.getPages());
        return new ResultUtils(200,"",pageUtil);
    }

    @Override
    public ResultUtils selectInventory() {
        List<Goods> list = this.list();
        return new ResultUtils(200,"",list);
    }

    @Transactional
    @Override
    public ResultUtils joinStorage(InventoryInfo inventoryInfo) {
        if (inventoryInfo.getStorageId() == null){
            return new ResultUtils(500,"请选择仓库ID！",null);
        }
        if ((goodsMapper.selectStorage(inventoryInfo.getGoodsId()).getStorageId() != null ?
                goodsMapper.updateStorage(inventoryInfo.getGoodsId(),inventoryInfo.getStorageId()) :
                goodsMapper.joinStorage(inventoryInfo.getGoodsId(),inventoryInfo.getStorageId())) > 0
        ){
            Goods goods = new Goods();
            BeanUtils.copyProperties(inventoryInfo,goods);
            UpdateWrapper uw = new UpdateWrapper();
            uw.eq("goods_id",goods.getGoodsId());
            this.update(goods,uw);
            return new ResultUtils(200,"入库成功！",null);
        }
        return new ResultUtils(500,"入库失败！",null);
    }

    @Override
    public ResultUtils outStorage(InventoryInfo inventoryInfo) {
        if (goodsMapper.outStorage(inventoryInfo.getGoodsId()) > 0){
            return new ResultUtils(200,"出库成功！",null);
        }
        return new ResultUtils(500,"出库失败！",null);
    }

    @Override
    public ResultUtils goodsIndex(GoodsInfo goodsInfo) {
        Page<Goods> page = new Page<>(goodsInfo.getPage(),goodsInfo.getRow());
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.like(goodsInfo.getGoodsName() != null && !goodsInfo.getGoodsName().equals(""),"goods_name",goodsInfo.getGoodsName());
        qw.like(goodsInfo.getGoodsType() != null && !goodsInfo.getGoodsType().equals(""),"goods_type",goodsInfo.getGoodsType());
        Page<Goods> pageList = this.page(page, qw);
        PageUtil<Goods> pageUtil = new PageUtil<>();
        pageUtil.setData(pageList.getRecords());
        pageUtil.setTotal(pageList.getTotal());
        pageUtil.setTotalPage(pageList.getPages());
        return new ResultUtils(200,"",pageUtil);
    }

    @Override
    public ResultUtils selectGoods() {
        return new ResultUtils(200,"",this.list());
    }

    @Transactional
    @Override
    public ResultUtils addStorage(InventoryInfo inventoryInfo) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(inventoryInfo,goods);
        if (this.save(goods)){
            QueryWrapper<Goods> qw = new QueryWrapper<>();
            qw.eq("goods_name",inventoryInfo.getGoodsName());
            Goods one = this.getOne(qw);
            if(goodsMapper.joinStorage(one.getGoodsId(), inventoryInfo.getStorageId()) > 0){
                return new ResultUtils(200,"添加成功！",null);
            }
        }
        return new ResultUtils(500,"添加失败！",null);
    }

    @Override
    public boolean updateGoodsNums(Integer goodsId, Integer goodsNums) {
        goodsMapper.updateGoodsNums(goodsId,goodsNums);
        return true;
    }

    @Override
    public ResultUtils deleteGoods(Integer goodsId) {
        this.removeById(goodsId);
        return new ResultUtils(200,"下架成功！",null);
    }

}
